package com.hdoit.ares.web.claim.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.claim.model.ClaimApplyfor;
import com.hdoit.ares.logical.claim.service.IClaimApplyforService;
import com.hdoit.ares.logical.viewmodel.claim.ClaimApplyforSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.SexStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年11月03日 09:09:48
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/claim/claimApplyfor/")
public class ClaimApplyforController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/claim/claimApplyfor/";
    }

    private IClaimApplyforService claimApplyforService;
    public IClaimApplyforService getClaimApplyforService() {
        return claimApplyforService;
    }
    @Resource
    public void setClaimApplyforService(IClaimApplyforService claimApplyforService) {
        this.claimApplyforService = claimApplyforService;
    }
    
   

	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="claimApplyforDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,ClaimApplyforSearch search,boolean ajaxPost){
        System.out.println("我的快递到了");
        pagerModel.setTotalAmount(claimApplyforService.queryCount(search));
        List<ClaimApplyfor> claimApplyfors= claimApplyforService.queryList(search,pagerModel);
        System.out.println("长度" + claimApplyfors.size());
        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("claimApplyfors", claimApplyfors);
        model.addAttribute("claimApplyforSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }
        System.out.println("这是哪");
        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String claimApplyforDetailed(Model model,String key){
        ClaimApplyfor claimApplyfor=new ClaimApplyfor();
        if(!StringUtils.isEmpty(key)){
            claimApplyfor=claimApplyforService.queryModel(key);
        }
        claimApplyfor.setSex(SexStatus.male);
        model.addAttribute("claimApplyfor", claimApplyfor);
        System.out.println("claimApplyfor" + claimApplyfor.getSex());
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,ClaimApplyfor claimApplyfor){
        if(StringUtils.isEmpty(claimApplyfor.getId())){
            claimApplyfor.setId(CommonUtils.getKeyId());
            claimApplyfor.setAid(CommonUtils.getKeyId());
            claimApplyfor.setCreateDate(DateUtils.getCurrent());
            claimApplyfor.setDeleteMark(YesOrNo.no);
            claimApplyfor.setModelStatus(ModelStatus.add);
            
        }else{
        	claimApplyfor.setTrackDate(DateUtils.getCurrent());
            claimApplyfor.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(claimApplyforService.applyChange(claimApplyfor,getLoginModel())){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("claimApplyfor", claimApplyfor);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(claimApplyforService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}